package problema043;


public class SubStringPandigital {
	
	static int[] numero = {0,1,2,3,4,5,6,7,8,9};
	static int[] div = {1,2,3,5,7,11,13,17};
	
	public static void main(String[] args){
		System.out.println(sumaPan());
	}
	
	public static long sumaPan(){
		long res = 0;
		do{
			if(cumple()) res += pasarNumero();
		}while(nextPermutation(numero));
		return res;		
	}
	
	public static boolean cumple(){
		for(int i = 0; i < div.length; i++){
			if(dameNum(i) % div[i] != 0) return false;
		}
		return true;		
	}
	
	public static int dameNum(int i){
		return numero[i] * 100 + numero[i+1] * 10 + numero[i+2];
	}
	
	public static long pasarNumero(){
		long res = 0, pos = 1;
		for(int i = numero.length -1; i >= 0 ; i--){
			res += numero[i] * pos;
			pos *= 10;
		}
		return res;
	}
	
	public static boolean nextPermutation(int[] p) {          
        int a = p.length - 2;          
        while (a >= 0 && p[a] >= p[a + 1]) {              
            a--;          
        }          
        if (a == -1) {              
            return false;          
        }
        int b = p.length - 1;          
        while (p[b] <= p[a]) {              
            b--;          
        }
        int t = p[a];          
        p[a] = p[b];          
        p[b] = t;          
        for (int i = a + 1, j = p.length - 1; i < j; i++, j--) {              
            t = p[i];              
            p[i] = p[j];
            p[j] = t;
        }
        return true;
    }

}
